package com.aeye.mbr.upms.server.controller.manage;

import com.aeye.mbr.upms.dao.model.UpmsDict;
import com.aeye.mbr.upms.rpc.api.UpmsDictService;
import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.aeye.mbr.common.base.BaseController;
import com.aeye.mbr.common.result.BootstrapTableParams;
import com.aeye.mbr.common.result.BootstrapTableResult;
import com.aeye.mbr.common.ui.validator.LengthValidator;
import com.aeye.mbr.upms.common.constant.UpmsResult;
import com.aeye.mbr.upms.common.constant.UpmsResultConstant;
import com.aeye.mbr.upms.dao.model.UpmsSystem;
import com.aeye.mbr.upms.rpc.api.UpmsSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 系统controller
 * Created by shuaeye.mbr on 2016/12/18.
 */
@Controller
@RequestMapping("/manage/system")
public class UpmsSystemController extends BaseController {

    private static Logger _log = LoggerFactory.getLogger(UpmsSystemController.class);

    @Autowired
    private UpmsSystemService upmsSystemService;

    @Autowired
    private UpmsDictService upmsDictService;

    @RequiresPermissions("upms:system:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/system/index.jsp";
    }

    @RequiresPermissions("upms:system:read")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(BootstrapTableParams params) {
        BootstrapTableResult result =  upmsSystemService.selectUpmsSystemByParams(params);
        System.out.println(result);
        return result;
    }

    @RequiresPermissions("upms:system:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "/manage/system/create.jsp";
    }

    @RequiresPermissions("upms:system:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(UpmsSystem upmsSystem) {
        ComplexResult result = FluentValidator.checkAll()
                .on(upmsSystem.getTitle(), new LengthValidator(1, 20, "标题"))
                .on(upmsSystem.getName(), new LengthValidator(1, 20, "名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        long time = System.currentTimeMillis();
        upmsSystem.setCtime(new Date());
        upmsSystem.setOrders(time);
        int count = upmsSystemService.insertSelective(upmsSystem);
        if(count > 0){
            //TODO 插入字典表 刷新字典缓存
        }
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @RequiresPermissions("upms:system:delete")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = upmsSystemService.deleteByPrimaryKeys(ids);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @RequiresPermissions("upms:system:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        UpmsSystem system = upmsSystemService.selectByPrimaryKey(id);
        modelMap.put("system", system);
        return "/manage/system/update.jsp";
    }

    @RequiresPermissions("upms:system:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, UpmsSystem upmsSystem) {
        ComplexResult result = FluentValidator.checkAll()
                .on(upmsSystem.getTitle(), new LengthValidator(1, 20, "标题"))
                .on(upmsSystem.getName(), new LengthValidator(1, 20, "名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        upmsSystem.setSystemId(id);
        int count = upmsSystemService.updateByPrimaryKeySelective(upmsSystem);
        if(count > 0){
            //TODO 更新字典表 刷新字典缓存
        }
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

}